#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

/*
 * IF YOU ARE TEACHING US C THEN DON'T MISLEAD US AND PASS THE ARGUMENT `-x c'
 *     TO THE g++ , please.
 * kthx
 */

typedef struct Matrix {
	unsigned int **data;
	int n, m;
} Matrix;

typedef struct Vector {
	Matrix *data; /* FIXME */
	int n, size;
} Vector;

/*
 * Cleanup after yourself.
 */
Matrix*
mk_Matrix(int n, int m)
{
	Matrix* a;
	register int i, j;

	if ((a = (Matrix*) malloc(sizeof(Matrix))) == NULL) {
		(void) fprintf(stderr, "mk_Matrix(): Out of memory\n");
		return NULL;
	}
	if ((a->data = (unsigned int**) malloc(n * sizeof(void*))) == NULL) {
		(void) fprintf(stderr, "mk_Matrix(): Out of memory\n");
		return NULL;
	}
	for (i = 0 ; i < n ; ++i) {
		if ((a->data[i] = (unsigned int*) malloc(m * sizeof(void*))) == NULL)
			(void) fprintf(stderr, "mk_Matrix():   Out of memory\n");
		for (j = 0 ; j < m ; ++j)
			a->data[i][j] = i * m + j;
	}
	a->n = n;
	a->m = m;
	return a;
}

void
free_Matrix(Matrix *a)
{
	register int i;
	for (i = 0 ; i < a->n ; ++i)
		free(a->data[i]);
	free(a->data);
}

void
Vector_init(Vector *v)
{
	v->n = 0;
	v->size = 2;
	v->data = (Matrix*) malloc(v->size * sizeof(Matrix));
	if (v->data == NULL)
		fprintf(stderr, "Init(): Out of memory\n");
}

void
Vector_append(Vector *v, Matrix* key)
{
	if (v->n >= v->size) { /* v->n will increase */
		v->size *= 2;
		v->data = (Matrix*) realloc(v->data, v->size * sizeof(Matrix));
	}
	v->data[v->n++] = *key;
}

void
Vector_print_Matrices(Vector *v)
{
	register int i, j, x;
	for (x = 0 ; x < v->n ; ++x) {
		for (i = 0 ; i < v->data[x].n ; ++i) {
			for (j=0 ; j < v->data[x].m ; ++j)
				printf("%4d ", v->data[x].data[i][j]);
			putchar('\n');
		}
		putchar('\n');
	}
}

void
transpose_Matrix_in_Vector(Vector *v, int idx, int row, int col, int size)
{
	Matrix *a = &v->data[idx];
	register int i, j;
	register unsigned int tmp;

	for (i=0; i < size-1 ; ++i)
		for (j=i+1; j < size ; ++j) {
			tmp = a->data[i+row][j+col];
			a->data[i+row][j+col] = a->data[j+row][i+col];
			a->data[j+row][i+col] = tmp;
		}
}


int
main(void)
{
	Vector *Vptr = NULL;
	Matrix *a = NULL;
	int cmd, n, m, i, size;

	freopen("matrices.in", "r", stdin);
	freopen("matrices.out", "w", stdout);

	if ((Vptr = (Vector*) malloc(sizeof(Vector))) == NULL) {
		(void) fprintf(stderr, "main(): Out of memory\n");
		return (1);
	}
	Vector_init(Vptr);
	while (scanf("%d", &cmd) == 1) {
		if (cmd == 1) {
			(void) scanf("%d%d", &n, &m);
			a = mk_Matrix(n, m);
			Vector_append(Vptr, a);
			free(a);
		} else if (cmd == 2) {
			(void) scanf("%d%d%d%d", &i, &n, &m, &size);
			transpose_Matrix_in_Vector(Vptr, i, n, m, size);
		}
	}
	Vector_print_Matrices(Vptr);

	/*
	 * Cleanup
	 */
	for (i = 0 ; i < Vptr->n ; ++i) {
		free_Matrix(&Vptr->data[i]);
	}
	free(Vptr->data);
	free(Vptr);
	return 0;
}
